import json, datetime, random
from flask import Blueprint, request, jsonify

from database import db

# 注册蓝图
coupon_bp = Blueprint('coupon', __name__)


@coupon_bp.route('/add/', methods=['POST', 'GET'])
def add():
    if request.method == "POST":
        # 获取vue发过来的参加
        data = json.loads(request.data)
        print(data)
        code = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S') + str(random.randint(10000, 99999))
        total = int(data['count'])
        # 用参数构造sql
        sql = "insert into coupon(code,man,jian,stime,etime,total) values('%s',%d,%d,'%s','%s',%d)" % (
            code, int(data['man']), int(data['jian']), data['stime'], data['etime'], total)
        # 调用database里面的添加insert方法进行添加
        try:
            db.insert(sql)
            return jsonify({'code': 200})
        except:
            return jsonify({'code': 10010})
    else:
        return '获取'